package com.zrrd.yunchmall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.Brand;
import com.zrrd.yunchmall.sale.entity.HomeBrand;
import com.zrrd.yunchmall.sale.entity.HomeNewProduct;
import com.zrrd.yunchmall.sale.service.IHomeBrandService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页推荐品牌表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/home/brand")
@Api(tags = "营销服务-品牌模块")
public class HomeBrandController {
    @Autowired
    private IHomeBrandService homeBrandService;
    @ApiOperation("查询优惠卷")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam int pageSize, @RequestParam int pageNum,
                               String brandName,Integer recommendStatus){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(brandName)){
            queryWrapper.like("brand_name",brandName);
        }
        if (recommendStatus!=null){
            queryWrapper.eq("recommend_status",recommendStatus);
        }
        return new ResponseResult<>(200,"查询成功！",
                homeBrandService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @PostMapping("/update/sort/{id}")
    @ApiOperation("查询修改排序")
    private ResponseResult update(@RequestParam @PathVariable("id") int id,@RequestParam int sort){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("sort",sort);
        updateWrapper.in("id",id);
        homeBrandService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
    @PostMapping("/delete")
    @ApiOperation("删除品牌推荐")
    private ResponseResult delete(@RequestParam String ids){
        for (String id:ids.split(",")) {
            homeBrandService.removeById(Long.valueOf(id));
        }
        return new ResponseResult<>(200,"删除成功!");
    }
    @PostMapping("/create")
    @ApiOperation("添加品牌推荐")
    private ResponseResult create(@RequestBody List<HomeBrand> homeBrandList){
        homeBrandList.forEach(homeBrand -> {
            QueryWrapper queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("brand_id",homeBrand.getBrandId());
            if (homeBrandService.count(queryWrapper)==0){
                homeBrand.setRecommendStatus(1);
                homeBrandService.save(homeBrand);
            }
        });
        return new ResponseResult<>(200,"添加成功!");
    }
    @PostMapping("/update/recommendStatus")
    @ApiOperation("修改品牌推荐状态")
    public ResponseResult updateRecommendStatus(String ids,Integer recommendStatus){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("recommend_status",recommendStatus);
        updateWrapper.in("id",ids.split(","));
        homeBrandService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
}
